﻿///////////////////////////////////////////////////////////////////////////////
// File:        ConnectionStream.hpp
// Author:      671643387@qq.com
// Description:
///////////////////////////////////////////////////////////////////////////////

#ifndef NIO_NET_CONNECTIONSTREAM_HPP_
#define NIO_NET_CONNECTIONSTREAM_HPP_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <boost/noncopyable.hpp>
#include <boost/shared_array.hpp>
#include <nio/net/Socket.hpp>

namespace nio
{
    namespace net
    {
        ///////////////////////////////////////////////////////////////////////
        class Connection;
        typedef boost::shared_array<char> ByteBuffer;
        ///////////////////////////////////////////////////////////////////////

        class ConnectionStream : public boost::noncopyable
        {
        protected:
            friend class Connection;
        protected:
            typedef ConnectionStream self;
        protected:
            ConnectionStream(Socket& sock, unsigned int size);
            ConnectionStream(Socket& sock, ByteBuffer buff, unsigned int size);
            virtual ~ConnectionStream();

        public:
            unsigned int capacity(void) { return buffSize_; }
            unsigned int size(void) { return size_; }
            unsigned int space(void) { return buffSize_ - size_; }

        protected:
            void reset(void)
            {
                begIndex_ = 0;
                endIndex_ = 0;
                size_ = 0;
            }

        protected:
            unsigned int read(char* buf, unsigned int length);
            unsigned int write(const char* buf, unsigned int length);

        protected:
            unsigned int peek(char* buf, unsigned int length);
            unsigned int skip(unsigned int length);

        protected:
            Socket&         sock_;

        private:
            unsigned int    buffSize_;
            unsigned int    begIndex_;
            unsigned int    endIndex_;
            unsigned int    size_;
            ByteBuffer      buffer_;
        };

    }
}

#endif //NIO_NET_CONNECTIONSTREAM_HPP_
